package com.csjy.migration.hnnd.business;

import com.csjy.migration.hnnd.*;
import com.csjy.migration.hnnd.annotation.BusinessDataConverter;

import javax.script.ScriptException;
import java.io.FileWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 增量和展期记录(授信展期记录)
 */
@BusinessDataConverter(
        insertSql = "insert into nd_credit_extension_schedule (ID, CREATE_NAME, CREATE_BY, CREATE_DATE, UPDATE_NAME, UPDATE_BY, UPDATE_DATE, SYS_ORG_CODE, SYS_COMPANY_CODE, BPM_STATUS, PRO_ID, AUDIT_FLOW_NO, ORIG_ENDDATE, PRO_BALANCE, EXTENSION_ENDDATE, CUSTOM_APPLY_REASON, BANK_OPINION, EFFECTIVE_DATE, PROC_ID, LENDING_ID)" +
                "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
        updateSql = "UPDATE nd_credit_extension_schedule SET CREATE_NAME = ?,CREATE_BY = ?,CREATE_DATE = ?,UPDATE_NAME = ?,UPDATE_BY = ?,UPDATE_DATE = ?,SYS_ORG_CODE = ?,SYS_COMPANY_CODE = ?,BPM_STATUS = ?,PRO_ID = ?,AUDIT_FLOW_NO = ?,ORIG_ENDDATE = ?,PRO_BALANCE = ?,EXTENSION_ENDDATE = ?,CUSTOM_APPLY_REASON = ?,BANK_OPINION = ?,EFFECTIVE_DATE = ?,PROC_ID = ?,LENDING_ID = ? WHERE ID = ?",
        findMigratedIdListSql = "SELECT ID FROM nd_credit_extension_schedule WHERE PRO_ID = ?"
)
public class NdCreditExtensionSchedule extends AbstractBusiness{

    public NdCreditExtensionSchedule(DbUtil sourceDbUtil, DbUtil targetDbUtil, SourceBusinessData sourceData, ProgramArgument programArgument, String projectId) throws SQLException {
        super(sourceDbUtil, targetDbUtil, sourceData, programArgument, projectId);
    }

    @Override
    public void migrate() throws ScriptException, SQLException, Exception {
        List<Map> BtItemDeferRecList = sourceData.getBtItemDeferRec();
        for(Map btItemDeferRec:BtItemDeferRecList){
            try {
                String sourceId = getSimpleId(btItemDeferRec.get("CREATE_TIME").toString(),btItemDeferRec.get("ITEMNO").toString());
                int needMigratedResult = this.needMigrated(sourceId);
                if(needMigratedResult == 0) {
                    continue;
                };
                // 单条插入或更新
                if (needMigratedResult == 1) {
                    this.targetDbUtil.updateByParams(updateSql, this.buildValues(btItemDeferRec, sourceId), true);
                    // 批量插入或更新 和 单条插入或更新 二选一
//             this.batchUpdateValues.add(this.buildValues(row));
                } else {
                    this.targetDbUtil.updateByParams(insertSql, this.buildValues(btItemDeferRec, sourceId));
                    // 批量插入或更新 和 单条插入或更新 二选一
//             this.batchInsertValues.add(this.buildValues(row));
                }

            } catch (Exception e) {
                throw new Exception(String.format("增量和展期记录(授信展期记录)[%s]迁移失败:%s\n", btItemDeferRec.get("ITEMNO"), e.getMessage()));
            }



    }
    }

    public List buildValues (Map btItemDeferRec, String id ) throws SQLException, ScriptException {
        List values = new ArrayList();
        //ID	主键
        values.add(id);
        //CREATE_NAME	创建人名称
        values.add(null);
        //CREATE_BY	创建人登录名称
        values.add(null);
        //CREATE_DATE	创建日期
        values.add(btItemDeferRec.get("CREATE_TIME"));
        //UPDATE_NAME	更新人名称
        values.add(null);
        //UPDATE_BY	更新人登录名称
        values.add(null);
        //UPDATE_DATE	更新日期
        values.add(btItemDeferRec.get("MODIFY_TIME"));
        //SYS_ORG_CODE	所属部门
        values.add(null);
        //SYS_COMPANY_CODE	所属公司
        values.add("002");
        //BPM_STATUS	流程状态
        values.add(null);
        //PRO_ID	项目ID
        values.add(btItemDeferRec.get("ITEMNO"));
        //AUDIT_FLOW_NO	审批流程
        values.add(btItemDeferRec.get("WORKFLOWNO")== null ? null :btItemDeferRec.get("WORKFLOWNO")+"-JW");
        //ORIG_ENDDATE	原到期时间
        values.add(btItemDeferRec.get("OLDASSUMPTIME"));
        //PRO_BALANCE	项目余额
        values.add(btItemDeferRec.get("ASKDEFERSUM"));
        //EXTENSION_ENDDATE	展期结束时间
        values.add(btItemDeferRec.get("ENDDEFERDATE"));
        //CUSTOM_APPLY_REASON	客户申请原因
        values.add(Tool.decodeUnicode(btItemDeferRec.get("CUSTOMREASON")));
        //BANK_OPINION	银行意见
        values.add(Tool.decodeUnicode(btItemDeferRec.get("BANKNOTION")));
        //EFFECTIVE_DATE	生效时间
        values.add(btItemDeferRec.get("VAILABLEDATE"));
        //PROC_ID	流程ID
        values.add(btItemDeferRec.get("WORKFLOWNO") == null ? null:btItemDeferRec.get("WORKFLOWNO")+"-JW");
        //LENDING_ID
        values.add(null);
        return values;
    }
}


